import wx 
import pymysql

db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='zthubo',charset='utf8')
cur = db.cursor()
class Example(wx.Frame): 
    def __init__(self, parent, title,size = (640,480)): 
        super(Example, self).__init__(parent, title = title,size=size) 
        sql = "SELECT `ID` FROM xf"
        cur.execute(sql)
        db.commit()
        self.Count=cur.rowcount

        p = wx.Panel(self) 
        mainbox = wx.BoxSizer(wx.VERTICAL) 

        t = wx.StaticBox(parent=p, id=-1, label='题库查询:') 
        tSizer = wx.StaticBoxSizer(t, wx.VERTICAL)

        box = wx.BoxSizer(wx.HORIZONTAL)
        self.nm1 = wx.TextCtrl(p,style=wx.TE_PROCESS_ENTER) 
        self.nm1.Bind(wx.EVT_TEXT_ENTER, self.OnClickFind)
        self.nm1.Bind(wx.EVT_LEFT_DCLICK,self.OnclickCls)
        
        self.l1 = wx.StaticText(p,label="总计:{count}".format(count=self.Count))
        FindButton = wx.Button(p, label='查找' )
        FindButton.Bind(wx.EVT_BUTTON, self.OnClickFind)

        box.Add(self.l1, proportion=1, flag=wx.ALL|wx.EXPAND|wx.ALIGN_LEFT, border=5)
        box.Add(FindButton, proportion=0, flag=wx.ALL|wx.ALIGN_RIGHT, border=5)
        tSizer.Add(self.nm1, proportion=1,flag=wx.ALL|wx.EXPAND, border=5) 
        tSizer.Add(box,proportion=0, flag=wx.ALL|wx.EXPAND, border=10)
        mainbox.Add(tSizer,proportion=0, flag=wx.ALL|wx.EXPAND, border=5)

        self.t2=wx.TextCtrl(p,style=wx.TE_MULTILINE)
        mainbox.Add(self.t2, proportion=1, flag=wx.ALL|wx.EXPAND, border=15)
        p.SetSizer(mainbox) 
        #self.Centre() 
        self.Show()
    def OnClickFind(self,event):
        #wx.MessageBox(self.nm1.GetValue())
        self.findq(self.nm1.GetValue())
        #self.t2.SetValue(self.nm1.GetValue())
    def findq(self,str):
        sql = "SELECT `Q`,`O`,`A` FROM xf where `Q` like '%{f}%'" .format(f=str)
        cur.execute(sql)
        results = cur.fetchall()
        t=""
        for row in results:
            t+=row[0]+"\n"
            t+=row[1]
            t+=row[2]+"\n\n"

        self.t2.SetValue(t)
        self.l1.SetLabel("总计:{c}    查询到:{f}".format(c=self.Count,f=cur.rowcount))
        t=""
    def OnclickCls(self,event):
        self.nm1.SetValue("")
        
app = wx.App() 
Example(None, title = '法宣在线题目查询') 
app.MainLoop()
db.close()